package com.rav.widget;

import com.rav.widget.R;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.widget.RemoteViews;

public class RAVWidget extends AppWidgetProvider {
	
	private static final ComponentName THIS_WIDGET = new ComponentName("com.rav.widget", "com.rav.widget.RAVWidget");
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//		context.startService(new Intent(context, UpdateService.class));
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
			
		switch (audioManager.getRingerMode()) {
			case AudioManager.RINGER_MODE_VIBRATE:
			case AudioManager.VIBRATE_TYPE_RINGER:
				views.setTextViewText(R.id.state, "ON");
	    		views.setImageViewResource(R.id.bar, R.drawable.bar_on);
	    		break;

			default:
				views.setTextViewText(R.id.state, "OFF");
	    		views.setImageViewResource(R.id.bar, R.drawable.bar_off);
	    		break;
		
		}
		
		appWidgetManager.updateAppWidget(appWidgetIds, views);
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

		views.setTextViewText(R.id.state, "onReceive");
		
		switch (audioManager.getRingerMode()) {
			case AudioManager.RINGER_MODE_VIBRATE:
			case AudioManager.VIBRATE_TYPE_RINGER:
				views.setTextViewText(R.id.state, "ON");
	    		views.setImageViewResource(R.id.bar, R.drawable.bar_on);
	    		break;
	
			default:
				views.setTextViewText(R.id.state, "OFF");
	    		views.setImageViewResource(R.id.bar, R.drawable.bar_off);
	    		break;
		}
		
//		updateWidget(context);
		AppWidgetManager.getInstance(context).updateAppWidget(THIS_WIDGET, views);
		
		super.onReceive(context, intent);
	}
	
	private void updateWidget(Context context) {
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		AppWidgetManager.getInstance(context).updateAppWidget(THIS_WIDGET, views);
	}
	
//	public static class UpdateService extends Service {
//		
//        @Override
//        public void onStart(Intent intent, int startId) {
//            // Build the widget update for today
//            RemoteViews updateViews = buildUpdate(this);
//
//            // Push update for this widget to the home screen
//            ComponentName thisWidget = new ComponentName(this, RAVWidget.class);
//            AppWidgetManager manager = AppWidgetManager.getInstance(this);
//            manager.updateAppWidget(thisWidget, updateViews);
//        }
//
//        
//        public RemoteViews buildUpdate(Context context) {
//        	RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);
//        	
//        	switch(currentRingerMode()) {
//	        	case R.id.onlyVibrate:
//	        	case R.id.ringAndVibrate:
//	        		updateViews.setImageViewResource(R.id.bar, R.drawable.bar_on);
//	        		break;
//	        		
//	        	case R.id.onlyRing:
//	        	case R.id.silent:
//	        		updateViews.setImageViewResource(R.id.bar, R.drawable.bar_off);
//	        		break;
//        	}
//        	
//            return updateViews;
//        }
//
//		@Override
//		public IBinder onBind(Intent arg0) {
//			// We don't need to bind to this service
//			return null;
//		}
//
//		protected int currentRingerMode() {
//			AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//			
//			if(audioManager == null) {
//				return -1;
//			}
//			
//			switch (audioManager.getRingerMode()) {
//			case AudioManager.RINGER_MODE_SILENT: 
//				return R.id.silent;
//			case AudioManager.RINGER_MODE_VIBRATE: 
//				return R.id.onlyVibrate;
//			}
//			
//			if (audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER))		
//				return R.id.ringAndVibrate;
//			
//			return R.id.onlyRing;
//		}
//    }
	
}
